package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.SDLUtil;
import com.joshondesign.sdljava.SDL_Surface;
import com.joshondesign.sdljava.gfx.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ParticlesDemo implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new ParticlesDemo());
    }

    public class Particle {
        public int x;
        public int y;
        public double vx;
        public double vy;
    }


    public void callback() throws Exception {
        Util.standard_init();
        SDL_Surface surface = Util.createResizableWindow(1024,768);
        final Scene scene = new Scene(surface);
        scene.setBackgroundFill(SColor.BLACK);
        Layer layer = new Layer(surface.getFormat());
        scene.add(layer);


        final int W = surface.getW();
        final int H = surface.getH();
        //final SFont font = new SFont(new File("src/resources/Junction.ttf"));
        layer.add(new Node() {
            List<Particle> particles = new ArrayList<Particle>();
            @Override
            public void draw(GFX gfx) {
                if(particles.size() < 1000) {
                    for(int i=0; i<10; i++) {
                        Particle p = new Particle();
                        p.x = W/2 + (int)((Math.random()*10)-5);
                        p.y = H/2 + (int)((Math.random()*10)-5);
                        p.vx = (Math.random()*10.0)-5.0;
                        p.vy = (Math.random()*10.0)-5.0;
                        particles.add(p);
                    }
                }

                gfx.setPaint(SColor.WHITE);
                for(Particle p : particles) {
                    p.x += p.vx;
                    p.y += p.vy;
                    if(p.y > H) p.y = 0;
                    if(p.y < 0) p.y = H;
                    if(p.x < 0) p.x = W;
                    if(p.x > W) p.x = 0;

                    gfx.fillRect(p.x,p.y,1,1);
                }

                //gfx.drawString(font, ""+particles.size(), 40,80);
            }
        });

        scene.start();
    }
}
